package com.android.accessibility.service;

import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;

import com.android.accessibility.R;
import com.android.accessibility.activity.HomeActivity;
import com.android.accessibility.function.WXOnekeyforwardingVideo;

public class WXObserverService extends Service {

    private int NOTI_SERV_RUNNING = R.string.wx_service_running;
    private WXOnekeyforwardingVideo mWXOnekeyforwardingVideo = null;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (mWXOnekeyforwardingVideo == null) {
            mWXOnekeyforwardingVideo = new WXOnekeyforwardingVideo(this);
        }
        mWXOnekeyforwardingVideo.stopOnekeyForward();
        mWXOnekeyforwardingVideo.startOnekeyForward();
        showNotification(NOTI_SERV_RUNNING, R.drawable.start);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mWXOnekeyforwardingVideo != null) {
            mWXOnekeyforwardingVideo.stopOnekeyForward();
            mWXOnekeyforwardingVideo = null;
        }
        stopForeground(true);
    }

    private void showNotification(int resId, int iconId) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.drawable.ic_launcher_small);
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_notify);
        builder.setLargeIcon(bmp);
        builder.setOngoing(true);
        String ticker = getResources().getString(resId);
        String text = getResources().getString(R.string.onekey_label);
        builder.setTicker(ticker);
        builder.setContentTitle(text);
        builder.setContentText(ticker);
        builder.setNumber(0);
        builder.setAutoCancel(true);
        Intent intent = new Intent(this, HomeActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);
        startForeground(resId, builder.build());
    }
}
